import os
import re
import sys


def rename_asset_file(dir_path: str, file_name: str, target_name: str, is_check_file: bool = False):
    """
    重命名资源文件
    """
    if dir_path == None or dir_path.strip() == "" or dir_path.strip() == ".":
        dir_path = "." + os.sep
    fs_list = os.listdir(dir_path)
    for f in fs_list:
        fs = os.path.join(dir_path, f)
        if os.path.exists(fs):
            if os.path.isdir(fs) and re.compile(r"(drawable|mipmap)(-(m|x*h)dpi)?").fullmatch(f):
                rename_asset_file(fs, file_name, target_name, True)
            elif is_check_file and os.path.isfile(fs) and f == file_name:
                tname = os.path.join(os.path.dirname(fs), target_name)
                os.rename(fs, tname)
                print("重命名: " + fs + " ---> " + tname)


def run_rename_tool():
    """
    运行重命名工具
    """
    while True:
        src_file_name: str = None
        while src_file_name == None or src_file_name.strip() == "":
            os.system("cls")
            src_file_name = input("要修改的文件名：")
        target_file_name: str = None
        while target_file_name == None or target_file_name.strip() == "":
            os.system("cls")
            target_file_name = input("目标文件名：")
        rename_asset_file('.', src_file_name, target_file_name)
        print("操作成功！")
        cmd: str = None
        while cmd == None or cmd not in ["G", "Q"]:
            os.system("cls")
            cmd = input("继续请输入[G],退出输入任意内容...").upper()
            if cmd == "G":
                run_rename_tool()
            else:
                sys.exit(0)
                break
        break


if __name__ == "__main__":
    run_rename_tool()
